package com.kfm.wiki.controller;

import com.kfm.wiki.entity.Doc;
import com.kfm.wiki.service.DocService;
import com.kfm.wiki.util.PageReq;
import com.kfm.wiki.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/doc")
public class DocController {

    @Autowired
    private DocService docService;

    @GetMapping("/list")
    public R list(Doc doc, @Valid PageReq page){
        List<Doc> list = docService.list(doc);
        return R.ok(list);
    }

    @PostMapping("/save")
    public R save(@RequestBody @Valid Doc doc){
        boolean flag = docService.save(doc);
        if (flag) {
            return R.ok("保存成功");
        }
        return R.fail("保存失败");
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
        boolean flag = docService.removeById(id);
        if (flag) {
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

    @DeleteMapping("/batchdelete")
    public R delete(Long[] ids){
        boolean flag = docService.removeBatchByIds(Arrays.asList(ids));
        if (flag) {
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

    @GetMapping("/vote/{id}")
    public R vote(@PathVariable("id") Long id){
        docService.increaseVoteCount(id);
        return R.ok("点赞成功");
    }

}
